<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>xmake</title>
  <link rel="icon" href="/assets/img/favicon.ico">
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
  <meta name="description" content="Description">
  <meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
  <link href="/assets/npm/github-markdown/github-markdown.min.css" rel="stylesheet">
  <style>
	.markdown-body {
		box-sizing: border-box;
		min-width: 200px;
		max-width: 980px;
		margin: 0 auto;
		padding: 45px;
	}

	@media (max-width: 767px) {
		.markdown-body {
			padding: 15px;
		}
	}
  </style>
</head>
<body>
<article class="markdown-body">
<h4>This is a mirror page, please see the original page: </h4><a href="https://xmake.io/#/zh-cn/package/local_package">https://xmake.io/#/zh-cn/package/local_package</a>
<div id="wwads-panel" class="wwads-cn wwads-vertical wwads-sticky" data-id="239" style="max-width:180px;bottom:20px;right:20px;width:200px;height:260px;background:#fff;position:fixed"></div>
</br>
    <script type="text/javascript" charset="UTF-8" src="https://cdn.wwads.cn/js/makemoney.js" async></script>
<script async type="text/javascript" src="//cdn.carbonads.com/carbon.js?serve=CE7I52QU&placement=xmakeio" id="_carbonads_js"></script>
<style>
#carbonads {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Oxygen-Sans, Ubuntu,
  Cantarell, "Helvetica Neue", Helvetica, Arial, sans-serif;
}

#carbonads {
  display: flex;
  max-width: 330px;
  background-color: hsl(0, 0%, 98%);
  box-shadow: 0 1px 4px 1px hsla(0, 0%, 0%, .1);
}

#carbonads a {
  color: inherit;
  text-decoration: none;
}

#carbonads a:hover {
  color: inherit;
}

#carbonads span {
  position: relative;
  display: block;
  overflow: hidden;
}

#carbonads .carbon-wrap {
  display: flex;
}

.carbon-img {
  display: block;
  margin: 0;
  line-height: 1;
}

.carbon-img img {
  display: block;
}

.carbon-text {
  font-size: 13px;
  padding: 10px;
  line-height: 1.5;
  text-align: left;
}

.carbon-poweredby {
  display: block;
  padding: 8px 10px;
  background: repeating-linear-gradient(-45deg, transparent, transparent 5px, hsla(0, 0%, 0%, .025) 5px, hsla(0, 0%, 0%, .025) 10px) hsla(203, 11%, 95%, .4);
  text-align: center;
  text-transform: uppercase;
  letter-spacing: .5px;
  font-weight: 600;
  font-size: 9px;
  line-height: 1;
}
</style>
    <h3 id="">默认打包格式</h3>
<p>2.5.5 版本之后，我们提供了一种新的本地包打包方案，将会更加无缝的对接 <code>add_requires</code> 和 <code>add_packages</code>。</p>
<p>我们执行 <code>xmake package</code> 命令就能够生成默认的新版打包格式。</p>
<pre><code class="lang-console">$ xmake package
package(foo): build/packages/f/foo generated
</code></pre>
<p>它将会产生 <code>build/packages/f/foo/xmake.lua</code> 文件，内容如下：</p>
<pre><code class="lang-lua">package("foo")
    set_description("The foo package")
    set_license("Apache-2.0")
    add_deps("add", "sub")

    on_load(function (package)
        package:set("installdir", path.join(os.scriptdir(), package:plat(), package:arch(), package:mode()))
    end)

    on_fetch(function (package)
        local result = {}
        result.links = "foo"
        result.linkdirs = package:installdir("lib")
        result.includedirs = package:installdir("include")
        return result
    end)
</code></pre>
<p>其实就是采用 <code>package()</code> 来定义描述本地包，就跟远程包一样。</p>
<p>而生成的目录结构如下：</p>
<pre><code class="lang-console">$ tree build/packages/f/foo/
build/packages/f/foo/
├── macosx
│   └── x86_64
│       └── release
│           ├── include
│           │   └── foo.h
│           └── lib
│               └── libfoo.a
└── xmake.lua
</code></pre>
<p>我们也能够使用 <code>add_requires</code>/<code>add_repositories</code> 接口来无缝集成这个包。</p>
<pre><code class="lang-lua">add_rules("mode.debug", "mode.release")

add_repositories("local-repo build")
add_requires("foo")

target("bar")
    set_kind("binary")
    add_files("src/*.cpp")
    add_packages("foo")
</code></pre>
<p>其中，add_repositories 配置指定本地包的仓库根目录，然后就可以通过 <code>add_requires</code> 来引用这个包了。</p>
<p>另外，生成的本地包，还有一个特性，就是支持 <code>target/add_deps</code>，会自动关联多个包的依赖关系，集成时候，也会自动对接所有依赖链接。</p>
<p>这里有完整的<a href="https://github.com/xmake-io/xmake/blob/dev/tests/actions/package/localpkg/test.lua">测试例子</a>。</p>
<pre><code class="lang-console">"/usr/bin/xcrun -sdk macosx clang++" -o build/macosx/x86_64/release/bar build/.objs/bar/macosx/x86_64/release/src/main.cpp.o -arch x86_64 -mmacosx-version-min=10.15 -isysroot
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX11.0.sdk -stdlib=libc++
 -L/Users/ruki/projects/personal/xmake/tests/actions/package/localpkg/bar/build/packages/f/foo/macosx/x86_64/release/lib
 -L/Users/ruki/projects/personal/xmake/tests/actions/package/localpkg/bar/build/packages/s/sub/macosx/x86_64/release/lib
 -L/Users/ruki/projects/personal/xmake/tests/actions/package/localpkg/bar/build/packages/a/add/macosx/x86_64/release/lib
 -Wl,-x -lfoo -lsub -ladd -lz
</code></pre>
<h3 id="">生成远程包</h3>
<p>除了本地包格式，<code>xmake package</code> 现在也支持生成远程包，便于用户将他们快速提交到远程仓库。</p>
<p>我们只需要在打包时候，修改包格式。</p>
<pre><code class="lang-console">$ xmake package -f remote
</code></pre>
<p>他也会产生 packages/f/foo/xmake.lua 文件。</p>
<pre><code class="lang-lua">package("foo")
    set_description("The foo package")
    set_license("Apache-2.0")
    add_deps("add", "sub")

    add_urls("https://github.com/myrepo/foo.git")
    add_versions("1.0", "<shasum256 or gitcommit>")

    on_install(function (package)
        local configs = {}
        if package:config("shared") then
            configs.kind = "shared"
        end
        import("package.tools.xmake").install(package, configs)
    end)

    on_test(function (package)
        -- TODO check includes and interfaces
        -- assert(package:has_cfuncs("foo", {includes = "foo.h"})
    end)
</code></pre>
<p>包定义配置相比本地包，多了实际的安装逻辑，以及 urls 和 versions 的设置，</p>
<p>我们也能够通过附加参数，去修改 urls，versions 等配置值，例如：</p>
<pre><code class="lang-console">$ xmake package -f remote --url=https://xxxx/xxx.tar.gz --shasum=xxxxx --homepage=xxxxx`
</code></pre>
<p>xmake 也会从 target 的 <code>set_license</code> 和 <code>set_version</code> 等配置中读取相关配置信息。</p>
<h3 id="cmake">从 CMake 中查找包</h3>
<p>现在 cmake 已经是事实上的标准，所以 CMake 提供的 find_package 已经可以查找大量的库和模块，我们完全复用 cmake 的这部分生态来扩充 xmake 对包的集成。</p>
<p>我们可以通过 <code>find_package("cmake::xxx")</code> 去借助 cmake 来找一些包，xmake 会自动生成一个 cmake 脚本来调用 cmake 的 find_package 去查找一些包，获取里面包信息。</p>
<p>例如：</p>
<pre><code class="lang-console">$ xmake l find_package cmake::ZLIB
{
  links = {
    "z"
  },
  includedirs = {
    "/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.
15.sdk/usr/include"
  },
  linkdirs = {
    "/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.
15.sdk/usr/lib"
  }
}
$ xmake l find_package cmake::LibXml2
{
  links = {
    "xml2"
  },
  includedirs = {
    "/Library/Developer/CommandLineTools/SDKs/MacOSX10.15.sdk/usr/include/libxml2"
  },
  linkdirs = {
    "/usr/lib"
  }
}
</code></pre>
<h4 id="">在项目中集成包</h4>
<p>如果我们在 xmake.lua 项目配置中集成查找 cmake 的依赖包，通常不需要直接使用 find_package，我们可以用更加通用、简单的包集成方式。</p>
<pre><code class="lang-lua">add_requires("cmake::ZLIB", {alias = "zlib", system = true})
target("test")
    set_kind("binary")
    add_files("src/*.c")
    add_packages("zlib")
</code></pre>
<p>我们指定 <code>system = true</code> 告诉 xmake 强制从系统中调用 cmake 查找包，如果找不到，不再走安装逻辑，因为 cmake 没有提供类似 vcpkg/conan 等包管理器的安装功能，<br>只提供了包查找特性。</p>
<h4 id="">指定版本</h4>
<pre><code class="lang-lua">add_requires("cmake::OpenCV 4.1.1", {system = true})
</code></pre>
<h4 id="">指定组件</h4>
<pre><code class="lang-lua">add_requires("cmake::Boost", {system = true, configs = {components = {"regex", "system"}})}
</code></pre>
<h4 id="">预设开关</h4>
<pre><code class="lang-lua">add_requires("cmake::Boost", {system = true, configs = {components = {"regex", "system"},
                                             presets = {Boost_USE_STATIC_LIB = true}}})
</code></pre>
<p>相当于内部调用 find_package 查找包之前，在 CMakeLists.txt 中预定义一些配置，控制 find_package 的查找策略和状态。</p>
<pre><code>set(Boost_USE_STATIC_LIB ON) -- will be used in FindBoost.cmake
find_package(Boost REQUIRED COMPONENTS regex system)
</code></pre><h4 id="">设置环境变量</h4>
<pre><code class="lang-lua">add_requires("cmake::OpenCV", {system = true, configs = {envs = {CMAKE_PREFIX_PATH = "xxx"}}})
</code></pre>
<h4 id="findfoocmake">指定自定义 FindFoo.cmake 模块脚本目录</h4>
<p>mydir/cmake_modules/FindFoo.cmake</p>
<pre><code class="lang-lua">add_requires("cmake::Foo", {system = true, configs = {moduledirs = "mydir/cmake_modules"}})
</code></pre>
<p>相关 issues: <a href="https://github.com/xmake-io/xmake/issues/1632">#1632</a></p>
<h4 id="">指定链接项</h4>
<p>对于 cmake 包，我们新增了 <code>link_libraries</code> 配置选项，让用户在查找使用 cmake 包的时候，可以自定义配置包依赖的链接库，甚至对 target 链接的支持。</p>
<pre><code class="lang-lua">add_requires("cmake::xxx", {configs = {link_libraries = {"abc::lib1", "abc::lib2"}}})
</code></pre>
<p>xmake 在查找 cmake 包的时候，会自动追加下面的配置，改进对 links 库的提取。</p>
<pre><code class="lang-cmake">target_link_libraries(test PRIVATE ABC::lib1 ABC::lib2)
</code></pre>
<h4 id="">指定搜索模式</h4>
<p>另外，我们增加的搜索模式配置：</p>
<pre><code class="lang-lua">add_requires("cmake::xxx", {configs = {search_mode = "config"}})
add_requires("cmake::xxx", {configs = {search_mode = "module"}})
add_requires("cmake::xxx") -- both
</code></pre>
<p>比如指定 config 搜索模式，告诉 cmake 从 <code>XXXConfig.cmake</code> 中查找包。</p>
<p>xmake 在查找 cmake 包的时候，内部会自动追加下面的配置。</p>
<pre><code class="lang-cmake">find_package(ABC CONFIG REQUIRED)
</code></pre>
</article>
</body>
</html>